package com.aaa.forum.services.impl;

import com.aaa.forum.common.AppResult;
import com.aaa.forum.common.ResultCode;
import com.aaa.forum.dao.CollectionMapper;
import com.aaa.forum.exception.ApplicationException;
import com.aaa.forum.model.Collection;
import com.aaa.forum.services.IBookmarkService;
import com.aaa.forum.services.ICollectionService;
import com.aaa.forum.uitls.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CollectionServiceImpl implements ICollectionService {

    @Resource
    private CollectionMapper collectionMapper;

    @Resource
    private IBookmarkService bookmarkService;

    @Override
    public List<Collection> selectALlByUserId(Long userId, Boolean isOwn) {
        if (userId == null || isOwn == null || userId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return collectionMapper.selectAllByUserId(userId, isOwn);
    }

    @Override
    public void create(Long userId, String name) {
        if (userId == null || userId <= 0 || StringUtil.isEmpty(name)) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Collection collection = new Collection();
        collection.setUserId(userId);
        collection.setName(name);
        collection.setState((byte) 0);
        collection.setState((byte) 0);
        Date date = new Date();
        collection.setUpdateTime(date);
        collection.setCreateTime(date);
        int row = collectionMapper.insertSelective(collection);
        if (row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
    }

    @Override
    public void modifyName(Long id, String name) {
        if (id == null || id <= 0 || StringUtil.isEmpty(name)) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Collection collection = new Collection();
        collection.setId(id);
        collection.setName(name);
        collection.setUpdateTime(new Date());
        int row = collectionMapper.updateByPrimaryKeySelective(collection);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void modifyState(Long id, Byte state) {
        if (id == null || id <= 0 || state == null || (state != 0 && state != 1)) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Collection collection = new Collection();
        collection.setId(id);
        collection.setState(state);
        collection.setUpdateTime(new Date());
        int row = collectionMapper.updateByPrimaryKeySelective(collection);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void deleteById(Long id) {
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Collection collection = new Collection();
        collection.setId(id);
        collection.setDeleteState((byte) 1);
        collection.setUpdateTime(new Date());
        int row = collectionMapper.updateByPrimaryKeySelective(collection);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        int count = bookmarkService.getCountByCollectionId(id);
        row = bookmarkService.moveToDefault(id);
        if (count != row) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public Collection selectById(Long id) {
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return collectionMapper.selectByPrimaryKey(id);
    }
}
